package com.qfmy.mystation.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.qfmy.mystation.R;
import com.qfmy.mystation.bean.LiveWallpaperBean;
import com.qfmy.mystation.ui.activity.LiveWallpaperDetailActivity;

import java.util.ArrayList;

public class LiveWallpaperAdapter extends RecyclerView.Adapter<LiveWallpaperAdapter.MyViewHolder> {
    private Context mContext;
    private ArrayList<LiveWallpaperBean.DataDTO.ListDTO> liveWallpaperList;

    public LiveWallpaperAdapter(Context mContext, ArrayList<LiveWallpaperBean.DataDTO.ListDTO> liveWallpaperList) {
        this.mContext = mContext;
        this.liveWallpaperList = liveWallpaperList;
    }

    @NonNull
    @Override
    public LiveWallpaperAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_img, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull LiveWallpaperAdapter.MyViewHolder holder, int position) {
        holder.bind(liveWallpaperList.get(position), position);
    }

    @Override
    public int getItemCount() {
        return liveWallpaperList == null ? 0 : liveWallpaperList.size();
    }

    @SuppressLint("NotifyDataSetChanged")
    public void addAll(ArrayList<LiveWallpaperBean.DataDTO.ListDTO> data) {
        if (liveWallpaperList != null) {
            liveWallpaperList.clear();
            liveWallpaperList.addAll(data);
            notifyDataSetChanged();
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    public void loadMore(ArrayList<LiveWallpaperBean.DataDTO.ListDTO> data) {
        liveWallpaperList.addAll(data);
        notifyDataSetChanged();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        private ImageView iv_item_img;
        private RecyclerView rv_tag;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            iv_item_img = itemView.findViewById(R.id.iv_item_img);
            rv_tag = itemView.findViewById(R.id.rv_tag);
        }

        void bind(LiveWallpaperBean.DataDTO.ListDTO data, int position) {
            Glide.with(mContext)
                    .load(data.getPreview())
                    .placeholder(R.drawable.inset)
                    .error(R.drawable.pic_loading_error)
                    .apply(new RequestOptions().centerCrop())
                    .into(iv_item_img);
            if (!TextUtils.isEmpty(data.getTag())) {
                rv_tag.setVisibility(View.VISIBLE);
                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
                linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
                rv_tag.setLayoutManager(linearLayoutManager);
                rv_tag.setAdapter(new TagListAdapter(data.getTag().split(","), R.layout.item_tag));
            } else {
                rv_tag.setVisibility(View.GONE);
            }
            iv_item_img.setOnClickListener(view -> {
                Intent intent = new Intent(mContext, LiveWallpaperDetailActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("live_wallpaper", liveWallpaperList);
                intent.putExtra("data", bundle);
                intent.putExtra("position", position);
                mContext.startActivity(intent);
            });
        }
    }
}
